﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows;
using System.Data;

namespace ET.ScoreTeacherClient.Print
{
    public interface IDocumentRenderer
    {
        void Render(FlowDocument doc, DataTable data, string strtitle);
    }
    public class DocumentRenderer : IDocumentRenderer
    {
        public void Render(FlowDocument doc, DataTable dt, string strtitle)
        {
            #region 标题赋值
            Run r = doc.FindName("titleRun") as Run;
            r.Text = strtitle;
            #endregion
            #region 表格赋值
            //表格
            Table tTable = doc.FindName("PrintTable") as Table;
            TableRow titleRow = new TableRow();
            titleRow.FontWeight = FontWeight.FromOpenTypeWeight(600);
            //根据DataTable创建列和列名称
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                //创建列 宽度没有设定
                TableColumn newColumn = new TableColumn();
                tTable.Columns.Add(newColumn);
                //创建列名称
                TableCell title = new TableCell(new Paragraph(new Run(dt.Columns[i].ColumnName)));
                title.Style = doc.Resources["BorderedCell"] as Style;
                titleRow.Cells.Add(title);
            }
            //向表中添加列名称
            TableRowGroup titlegroup = new TableRowGroup();
            titlegroup.Rows.Add(titleRow);
            tTable.RowGroups.Add(titlegroup);
            //向表中添加数据
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TableRow cellRow = new TableRow();
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    TableCell cell = new TableCell(new Paragraph(new Run(dt.Rows[i][j].ToString())));
                    cell.Style = doc.Resources["BorderedCell"] as Style;
                    cellRow.Cells.Add(cell);
                }
                TableRowGroup cellgroup = new TableRowGroup();
                cellgroup.Rows.Add(cellRow);
                tTable.RowGroups.Add(cellgroup);
            }
            #endregion                        
        }
    }
}
